<template>
  <!-- 使用 Ant Design 的 ConfigProvider 组件，设置语言环境 -->
  <a-config-provider :locale="antDLocale">
    <!-- Nuxt 布局组件 -->
    <NuxtLayout>
      <!-- Nuxt 页面组件 -->
      <NuxtPage />
    </NuxtLayout>
  </a-config-provider>
</template>

<script setup lang="ts">
import {
  getCurrentInstance,
  nextTick,
  onBeforeMount,
  onMounted,
  provide,
  ref,
  watch,
} from "vue";
import { useCountryStore, useGlobalStore, useMemberStore } from "./store";
import { useRoute } from "vue-router";
import languageList from "./data/languageList";
import { postChangeLanguage, postSessionId } from "./api/api";
import { message } from "ant-design-vue";
import { postSessionLanguage } from "./api/api";
import { useCookie, useHead, useState } from "nuxt/app";
import http2 from "./utils/http2";
import http from "./utils/http";

// 使用 i18n 国际化
const { locale, locales, setLocale } = useI18n();
// 获取当前实例的翻译函数
const $t = getCurrentInstance()?.appContext.config.globalProperties.$t;
// 获取当前路由
const route = useRoute();
// 初始化 store
const countryStore = useCountryStore();
const globalStore = useGlobalStore();
const memberStore = useMemberStore();

// postChangeLanguage(useCookie("lang").value || "en");

// 获取应用数据
const getAppData = async () => {
  await globalStore.getGlobalData();
  countryStore.getCountryData();
};

// 配置网页标签标题
const setHeader = () => {
  useHead({
    title: () => globalStore.configData?.web_name || "SC AEROSPACE",
    meta: () => [
      {
        name: "keywords",
        content: globalStore.configData?.web_keywords || "",
      },
      {
        name: "description",
        content: globalStore.configData?.web_describe || "",
      },
    ],
  });
};

// 初始化应用加载语言
const initAppLoadLang = async () => {
  let appLanguage = useCookie("lang");
  if (!appLanguage.value) {
    appLanguage.value = "en";
  }
  if (process.client) {
    try {
      let client_lang = memberStore.AppLang;
      if (appLanguage.value == client_lang) {
        return;
      } else {
        location.reload();
      }
    } catch (error) {
      if (!appLanguage.value) {
        appLanguage.value = "en";
      }
    }
  }

  // 服务端获取数据 SSR 模式下
  if (process.server) {
    getAppData();
    setHeader();
    setAppLocale();
  }
};

// 设置应用语言环境
const setAppLocale = () => {
  let lang = useCookie("lang").value;
  memberStore.setAppLang(lang as string);
  setLocale(lang);
  setTimeout(() => {
    antDLocale.value = useAntdLocale(locale.value);
  });
};
initAppLoadLang();

// 重置页面显示（处理移动端和 PC 端路由）
const resetPageShow = () => {
  if (!isMobile()) {
    if (route.path.includes("/mobile")) {
      goto("/");
    }
  }
};

// Ant Design 语言环境
const antDLocale = ref();

// 组件挂载时执行
onMounted(async () => {
  // 在客户端环境下执行
  await nextTick();
  window.$vueLang = $t;
  setAppLocale();
  resetPageShow();
  routesAuth();
  window.addEventListener("resize", () => {
    resetPageShow();
  });
});
// 权限验证
let auth_routes = ["/member", "/profile", "/pdf", "/new_profile"];
const routesAuth = async () => {
  if (process.client) {
    await nextTick();
    if (!memberStore.profile) {
      memberStore.getProfileLocal();

      if (memberStore.profile) {
        memberStore.getProfileData();
      }
    }
    auth_routes.forEach((path) => {
      if (route.path.includes(path)) {
        if (!memberStore.profile) {
          message.warning($t("message.wait_login"));
          goto(
            "/login",
            { redirect: route.fullPath },
            { redirect: true, noAuth: true }
          );
        }
      }
    });
  }
};

// 监听路由变化
watch(route, async () => {
  await initAppLoadLang();
  setAppLocale();
  routesAuth();
});
</script>
